﻿using B.S.AuthCenter.Api.Read.Application.Command.Users;
using B.S.AuthCenter.Api.Read.DTOs;
using B.S.AuthCenter.Common;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.Data;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace B.S.AuthCenter.Api.Read.Application.Handler.Users
{
    public class LoginUserHandler : IRequestHandler<LoginUserCommand, ApiResult<LoginDTO>>
    {
        IBaseRepository<UserModel> userRepo;
        IBaseRepository<UserRoleModel> userRoleRepo;
        IBaseRepository<RoleModel> roleRepo;
        JwtTools jwtTools;
        IConfiguration configuration;

        public LoginUserHandler(IBaseRepository<UserModel> userRepo, IBaseRepository<UserRoleModel> userRoleRepo, IBaseRepository<RoleModel> roleRepo, JwtTools jwtTools, IConfiguration configuration)
        {
            this.userRepo = userRepo;
            this.userRoleRepo = userRoleRepo;
            this.roleRepo = roleRepo;
            this.jwtTools = jwtTools;
            this.configuration = configuration;
        }

        public  Task<ApiResult<LoginDTO>> Handle(LoginUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<LoginDTO> res=new ApiResult<LoginDTO>();
            var users=userRepo.GetAll().Where(c=>c.UserName==request.UserName).FirstOrDefault();
            if (users == null)
            {
                res.Msg = "账户不存在";
                res.Code=ApiEnum.Fail;
                return Task.FromResult(res);
            }
            if(users.Pwd!=request.Pwd)
            {
                res.Msg = "密码错误";
                res.Code=ApiEnum.Fail;
                return Task.FromResult(res);
            }
            LoginDTO loginDTO = new LoginDTO();
            loginDTO.UserName = request.UserName;
            loginDTO.Uid=users.Id;
            loginDTO.Rid = new List<int>();
            var userrole = userRoleRepo.GetAll().Where(c => c.Uid == users.Id).ToList();
            var departmentId = userRepo.GetAll().Where(c => c.Id == users.Id).Select(c => c.DepartmentId).FirstOrDefault();
            loginDTO.DepartmentId = departmentId;
            foreach (var item  in userrole)
            {
                var role = roleRepo.GetAll().Where(c => !c.IsDeleted).FirstOrDefault(x => x.Id == item.Rid);
                if(role==null)
                {
                    continue;
                }
                loginDTO.Rid.Add(role.Id);
            }
            loginDTO.Rid=loginDTO.Rid.Distinct().ToList();
            loginDTO.DepartmentIds = new List<int>();
            var departIds = userRepo.GetAll().Where(c => c.DepartmentId == users.DepartmentId).ToList();
            foreach(var item in departIds)
            {
                var userName = userRepo.GetAll().Where(c => !c.IsDeleted).FirstOrDefault(x => x.Id == item.Id);
                if(userName==null)
                {
                    continue;
                }    
                loginDTO.DepartmentIds.Add(userName.Id);
            }
            loginDTO.DepartmentIds=loginDTO.DepartmentIds.Distinct().ToList();
            loginDTO.UserIds = new List<int>();
            loginDTO.Names = new List<string>();
            foreach (var item in loginDTO.DepartmentIds)
            {
                var roles=userRoleRepo.GetAll().Where(c=>!c.IsDeleted).FirstOrDefault(x=>x.Uid==item);
                if(roles==null)
                {
                    continue;
                }
                loginDTO.UserIds.Add(roles.Uid);
            }
            loginDTO.UserIds=loginDTO.UserIds.Distinct().ToList();
            foreach (var item in loginDTO.UserIds)
            {
                var username = userRepo.GetAll().Where(c => !c.IsDeleted).FirstOrDefault(x => x.Id == item);
                if(username==null)
                {
                    continue;
                }
                loginDTO.Names.Add(username.UserName);
            }
            var token = jwtTools.JwtNewString(configuration, users.Id.ToString());
            loginDTO.token = token;
            res.Msg = "登录成功";
            res.Code=ApiEnum.Success;
            res.Datas=loginDTO;
            return Task.FromResult(res);
        }
       

    }
}
